var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"],
              ["blue", "#000080"], ["purple", "#800080"]];
var options = {};
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var colorIndex_ = 0;
var featureTable_;
var map;
var pts = [];
var stArr = [];
var c = 0;
function select(buttonId) {
  document.getElementById("hand_b").className="unselected";
  document.getElementById("shape_b").className="unselected";
  document.getElementById("line_b").className="unselected";
  document.getElementById("placemark_b").className="unselected";
  document.getElementById(buttonId).className="selected";
}

function stopEditing() {
  select("hand_b");
}

function getColor(named) {
  return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}

function getIcon(color) {
  var icon = new GIcon();
  icon.image = "http://google.com/mapfiles/ms/micons/" + color + ".png";
  icon.iconSize = new GSize(32, 32);
  icon.iconAnchor = new GPoint(15, 32);
  return icon;
}
  var tmpPoint = [];
function startShape() {
  select("shape_b");
	
  var tmpPt = '';
  GEvent.addListener(map, "click", function(overlay, pt) {
	if(pt)
		tmpPt += pt+"#";
  });
  
  var color = getColor(false);
  var polygon = new GPolygon([], color, 2, 0.7, color, 0.2);
  startDrawing(polygon,tmpPt,shapeCounter_, "Shape " + (++shapeCounter_), function() {
    var cell = this;
	alert(polygon.containsLatLng(coordinate));
    var area = polygon.getArea();
    cell.innerHTML = (Math.round(area / 10000) / 100) + "km<sup>2</sup>";
  }, color);
    
}

function startLine() {
  select("line_b");
  var color = getColor(false);
  var line = new GPolyline([], color);
  startDrawing(line, "Line " + (++lineCounter_), function() {
    var cell = this;
    var len = line.getLength();
    cell.innerHTML = (Math.round(len / 10) / 100) + "km";
  }, color);
}

function addFeatureEntry(name, color) {
  currentRow_ = document.createElement("tr");
  var colorCell = document.createElement("td");
  currentRow_.appendChild(colorCell);
  colorCell.style.backgroundColor = color;
  colorCell.style.width = "1em";
  var nameCell = document.createElement("td");
  currentRow_.appendChild(nameCell);
  nameCell.innerHTML = name;
  var descriptionCell = document.createElement("td");
  currentRow_.appendChild(descriptionCell);
  featureTable_.appendChild(currentRow_);
  return {desc: descriptionCell, color: colorCell};
}
function checkThis(type,pt) 
{
	var ptsnew = pt.y + ',' + pt.x;
	pts[type] = ptsnew;
	if((stArr[type]))
	{
		myHtml="<b>Name It:</b><br><br><input type='text' name='txtPoint"+(type)+"' id='txtPoint"+(type)+"' value='"+(stArr[type])+"'/>";
	}
	else
	{
		myHtml="<b>Name It:</b><br><br><input type='text' name='txtPoint"+(type)+"' id='txtPoint"+(type)+"' value='Point"+(type+1)+"'/>";
	}
	myHtml+=" <a href='#' class='ui-ui-button' onclick='setStopPoint("+(type+1)+");'> Update</a>";				
	map.openInfoWindow(pt, myHtml);
}
var stopName=/^[a-zA-Z0-9][a-zA-Z0-9 ]*$/;
function validThis(v1)
{
 	if(document.getElementById('txtPoint'+(v1-1)).value=="")
	{
		showMsg('Stop Name required');
		document.getElementById('txtPoint'+(v1-1)).focus();
		return false;
	}
  	else if (document.getElementById('txtPoint'+(v1-1)).value.indexOf(' ') == 0) 
	{
		showMsg('Spaces not allowed in Stop Name');
		document.getElementById('txtPoint'+(v1-1)).focus();
		document.getElementById('txtPoint'+(v1-1)).select();
		return false;
	}
	else if (!stopName.test(document.getElementById('txtPoint'+(v1-1)).value)) 
	{
		showMsg('Only alphanumeric allowed');
		document.getElementById('txtPoint'+(v1-1)).focus();
		document.getElementById('txtPoint'+(v1-1)).select();
		return false;	
	}
	return true;
}
var ajax1=new sack();
function setStopPoint(v1)
{
	var flg1=validThis(v1);
	if(flg1)
	{
		stArr[v1-1]=document.getElementById('txtPoint'+(v1-1)).value;
		ajax1.requestFile = '../../user/ajax_server.php?addGeoPoint=y&param='+pts[v1-1]+'&name='+stArr[v1-1]+'&radius='+radiusArr[v1-1];
		document.write(ajax1.requestFile);
		//ajax1.onCompletion = function(){insertPoint()};
		//ajax1.runAJAX();
		
}
function insertPoint()
{
	//showMsg(ajax1.response);
	if(ajax1.response ==1 )
		showMsg("Point added Successfully");
	else if(ajax1.response ==2 )
		showMsg("Point updated Successfully");
	else if(ajax1.response ==0 )
		showMsg("Point no added");
	
	myMap.closeInfoWindow();
	
	for(h=0;h<stArr.length;h++)
	{
		if(stArr[h])
		{
			document.getElementById('spanPTNameID_'+h).innerHTML =stArr[h];
			document.getElementById('spanRadID_'+h).innerHTML =radiusArr[h];	
		}
	}
	
	}
}

function startDrawing(poly,tmpPt,c, name, onUpdate, color) {
  map.addOverlay(poly);
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  GEvent.addListener(poly, "endline", function() {
    select("hand_b");
    var cells = addFeatureEntry(name, color);
    GEvent.bind(poly, "lineupdated", cells.desc, onUpdate);
    GEvent.addListener(poly, "click", function(latlng, index) {
	alert(tmpPt)
	if(c==0)
	{
		pts[c] = latlng.y + ',' + latlng.x ;
		myHtml="<b>Name It:</b><br><br><input type='text' name='txtPoint"+c+"' id='txtPoint"+c+"' value='Point"+(c+1)+"'/>";
		myHtml+=" <a href='#' onclick='setStopPoint(c);'>Add</a>";				
		map.openInfoWindow(latlng, myHtml);
	}
	else
	{
		pts[c] = latlng.y + ',' + latlng.x ;
		myHtml="<b>Name It:</b><br><br><input type='text' name='txtPoint"+c+"' id='txtPoint"+c+"' value='Point"+(c+1)+"' />";
		myHtml+=' <a href="#" onclick="setStopPoint(c);">Add</a>';				
		map.openInfoWindow(latlng, myHtml);
	}
	
	//checkThis(c,latlng);
      if (typeof index == "number") {
        poly.deleteVertex(index);
      } else {
        var newColor = getColor(false);
        cells.color.style.backgroundColor = newColor
        poly.setStrokeStyle({color: newColor, weight: 4});
      }
    });
  });
}

function placeMarker() {
  select("placemark_b");
  var listener = GEvent.addListener(map, "click", function(overlay, latlng) {
    if (latlng) {
      select("hand_b");
      GEvent.removeListener(listener);
      var color = getColor(true);
      var marker = new GMarker(latlng, {icon: getIcon(color), draggable: true});
      map.addOverlay(marker);
      var cells = addFeatureEntry("Placemark " + (++markerCounter_), color);
      updateMarker(marker, cells);
      GEvent.addListener(marker, "dragend", function() {
        updateMarker(marker, cells);
      });
      GEvent.addListener(marker, "click", function() {
        updateMarker(marker, cells, true);
      });
    }
  });
}

function updateMarker(marker, cells, opt_changeColor) {
  if (opt_changeColor) {
    var color = getColor(true);
    marker.setImage(getIcon(color).image);
    cells.color.style.backgroundColor = color;
  }
  var latlng = marker.getPoint();
  cells.desc.innerHTML = "(" + Math.round(latlng.y * 100) / 100 + ", " +
  Math.round(latlng.x * 100) / 100 + ")";
}


function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.clearOverlays();
    featureTable_ = document.getElementById("featuretbody");
    select("shape_b");
  }
}
asd